﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TaDMS.Commons
{
    public class DateTimeUtility
    {
        public static List<string> WeekInYear()
        {
            try
            {
                var result = new List<string>();
                var weekCount = 0;
                var currentYear = DateTime.Now.Year;
                var beginDate = new DateTime(currentYear, 01, 01);
                var endDate = new DateTime(currentYear + 1, 01, 01);

                var monday = DateTime.Today;
                var friday = DateTime.Today;

                while (beginDate < endDate)
                {
                    beginDate = beginDate.AddDays(1);

                    if (beginDate.DayOfWeek == DayOfWeek.Monday)
                    {
                        monday = beginDate;
                    }
                    else if (beginDate.DayOfWeek == DayOfWeek.Friday)
                    {
                        friday = beginDate;
                    }
                    else if (beginDate.DayOfWeek == DayOfWeek.Saturday)
                    {
                        weekCount++;
                        result.Add("Tuần " + weekCount + " : từ " + monday.ToShortDateString() + " đến " + friday.ToShortDateString());
                    }
                }

                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static List<ListWeek> WeekInYear(int currentYear)
        {
            try
            {
                var result = new List<ListWeek>();
                var weekCount = 0;

                var beginDate = new DateTime(currentYear, 01, 01);
                var endDate = new DateTime(currentYear + 1, 01, 01);

                var monday = DateTime.Today;
                var friday = DateTime.Today;

                while (beginDate < endDate)
                {
                    beginDate = beginDate.AddDays(1);

                    if (beginDate.DayOfWeek == DayOfWeek.Monday)
                    {
                        monday = beginDate;
                    }
                    else if (beginDate.DayOfWeek == DayOfWeek.Friday)
                    {
                        friday = beginDate;
                    }
                    else if (beginDate.DayOfWeek == DayOfWeek.Saturday || (beginDate.Day == 31 && beginDate.Month == 12))
                    {
                        weekCount++;
                        result.Add(new ListWeek(){ StartWeek = monday, EndWeek = friday, NumberOfWeek = weekCount, Year = currentYear});
                    }
                }

                result[0].StartWeek = new DateTime(currentYear, 01, 01);

                result[weekCount - 1].EndWeek = new DateTime(currentYear, 12, 31);

                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    public class ListWeek
    {
        public DateTime StartWeek { get; set; }
        public DateTime EndWeek { get; set; }
        public int NumberOfWeek { get; set; }
        public int Year { get; set; }
    }
}